package pl.edu.pk.mech.kkbus.controller;

import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;

import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pl.edu.pk.mech.kkbus.model.ResponseStatus;
import pl.edu.pk.mech.kkbus.component.MyEmailSender;

@Controller
@RequestMapping("/test")
public class TestController {

    private static final Logger log = Logger.getLogger(TestController.class.getName());
    
    @Autowired
    MyEmailSender emailSender;
    
    @RequestMapping(value = "/email", method = RequestMethod.GET)
    public @ResponseBody ResponseStatus sendExampleEmail() {
    
        emailSender.send("macin90@gmail.com", "KKBus", "Lorem ipsum...");
        
        return null;
    }
    
    @RequestMapping(value = "/fetch", method = RequestMethod.GET)
    public @ResponseBody ResponseStatus fetchURL() throws MalformedURLException, IOException {
        
        URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
        
        HTTPRequest request = new HTTPRequest(new URL("http://jackson.netserwer.pl/kkbus/"), HTTPMethod.POST);
        String to = "macin90@gmail.com";
        String subject = "KKBus Test";
        String message = "Lorem ipsum...";
        String body = "to=" + to + "&subject=" + subject + "&message=" + message;
        //request.setPayload(body.getBytes());
        
        fetcher.fetch(request);
        
        return null;
    }
}
